package top.xin1901.j2ecd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import top.xin1901.j2ecd.beans.Role;
import top.xin1901.j2ecd.common.response.R;
import top.xin1901.j2ecd.service.RoleService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author HeYunjia
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @PostMapping
    public R postRoleOne(@RequestBody Role role) {
        return R.success()
                .add("role", roleService.insertOne(role));
    }

    @GetMapping
    public R getRoleAll() {
        return R.success()
                .add("roles", roleService.selectAll());
    }

    @GetMapping("/{pageNumber}/{pageSize}")
    public R getRolePage(@PathVariable Integer pageNumber,
                         @PathVariable Integer pageSize) {

        PageHelper.startPage(pageNumber, pageSize);

        List<Role> lists = roleService.selectAll();

        return R.success()
                .add("page", new PageInfo<>(lists));
    }

    @GetMapping("/{roleId}")
    public R getRoleOne(@PathVariable Long roleId) {
        return R.success()
                .add("role", roleService.selectOne(roleId));
    }

}
